package com.baofu.paycore.manager.storage.impl;

import com.baofu.paycore.common.enums.UsableFlagEnum;
import com.baofu.paycore.common.utils.Validate;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.manager.storage.DispatchManager;
import com.system.commons.exception.BizServiceException;
import com.system.dispatch.dal.mapper.BizCmdMapper;
import com.system.dispatch.dal.model.BizCmdDO;
import com.system.dispatch.dal.model.PageParamDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * dispatch服务
 *
 * <p>
 *  1.更新dispatch信息
 *  2.依ID查询dispatch信息
 *  3.查询dispatch总数
 *  4.分页查询dispatch信息
 * </p>
 * User: xin deng  Date: 2016/12/6 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Component
public class DispatchManagerImpl implements DispatchManager {

    /**
     * 业务命令mapper
     */
    @Autowired
    private BizCmdMapper bizCmdMapper;

    /**
     * 更新dispatch信息
     *
     * @param bizCmdDO 请求参数
     */
    @Override
    public void updateDispatch(BizCmdDO bizCmdDO) {

        int count = bizCmdMapper.update(bizCmdDO);
        Validate.checkUpdate(count);
    }

    /**
     * 依ID查询dispatch信息
     *
     * @param id ID
     * @return dispatch信息
     */
    @Override
    public BizCmdDO getDispatchById(String id) {

        BizCmdDO bizCmdDO = bizCmdMapper.selectById(id);
        if (bizCmdDO == null){
            log.error("依ID查询dispatch信息为空,ID:{}", id);
            throw new BizServiceException(PayCoreErrorCode.EMPTY_QUERY_RESULT, "依ID未查到dispatch信息");
        }
        return bizCmdDO;
    }

    /**
     * 查询dispatch总数
     *
     * @param bizCmdDO 请求参数
     * @return 总数
     */
    @Override
    public int getDispatchCount(BizCmdDO bizCmdDO) {

        return bizCmdMapper.selectCount(bizCmdDO);
    }

    /**
     * 分页查询dispatch信息
     *
     * @param bizCmdDO      请求参数
     * @param pageBegin     开始页
     * @return              dispatch信息
     */
    @Override
    public List<BizCmdDO> getDispatchPage(BizCmdDO bizCmdDO, int pageBegin, int pageEnd) {
        bizCmdDO.setStartRow(pageBegin);
        bizCmdDO.setPageCount(pageEnd);
        return bizCmdMapper.selectByPage(bizCmdDO);
    }
}
